//
//  ViewController.m
//  JSON2ObjCClass
//
//  Created by Captain Black on 15-2-22.
//  Copyright (c) 2015年 aipai. All rights reserved.
//

#import "ViewController.h"

#import "JSONKit.h"

@interface ViewController ()
@property (unsafe_unretained) IBOutlet NSTextView *JSONTextView;
@property (unsafe_unretained) IBOutlet NSTextView *ObjCTextView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
}

- (void)loadView
{
    [super loadView];
    [self mInit];
}

- (void)mInit
{
    self.JSONTextView.automaticDashSubstitutionEnabled = NO;
    self.JSONTextView.automaticQuoteSubstitutionEnabled = NO;
    self.ObjCTextView.automaticDashSubstitutionEnabled = NO;
    self.ObjCTextView.automaticQuoteSubstitutionEnabled = NO;
    self.JSONTextView.font = self.ObjCTextView.font = [NSFont systemFontOfSize:16];
}

- (IBAction)actionForGenerateButton:(NSButton *)sender {
    NSMutableString *classString = [[NSMutableString alloc] initWithString:@"/**\n\tGenerated by JSON2ObjCClass\n*/\n@interface <#ClassName#> : <#SuperClassName#>\n\n"];
    
    NSString *json = self.JSONTextView.textStorage.string;
    NSDictionary *dict = [json objectFromJSONString];
    id value = nil;
    for (NSString *key in dict) {
        value = dict[key];
        if ([value isKindOfClass:[NSString class]]) {
            [classString appendFormat:@"@property (nonatomic, strong) %@ *<#%@#>;\n", @"NSString", key];
        } else if ([value isKindOfClass:[NSDictionary class]]) {
            [classString appendFormat:@"@property (nonatomic, strong) %@ *<#%@#>;\n", @"NSDictionary", key];
        } else if ([value isKindOfClass:[NSArray class]]) {
            [classString appendFormat:@"@property (nonatomic, strong) %@ *<#%@#>;\n", @"NSArray", key];
        } else if ([value isKindOfClass:[NSNumber class]]) {
            if (!strcmp([value objCType], @encode(CGFloat))) {
                [classString appendFormat:@"@property (nonatomic, assign) %@ <#%@#>;\n", @"CGFloat", key];
            } else if (!strcmp([value objCType], @encode(NSInteger))) {
                [classString appendFormat:@"@property (nonatomic, assign) %@ <#%@#>;\n", @"NSInteger", key];
            } else if (!strcmp([value objCType], @encode(BOOL))) {
                [classString appendFormat:@"@property (nonatomic, assign) %@ <#%@#>;\n", @"BOOL", key];
            }
        } else {
            [classString appendFormat:@"@property (nonatomic, strong) %@ *<#%@#>;\n", @"<#NSObject#>", key];
        }
    }
    
    [classString appendString:@"\n@end"];
    
    [self.ObjCTextView.textStorage setAttributedString:[[NSAttributedString alloc] initWithString:classString]];
}
@end
